package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.AddressBookService;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ck
 * @date 2021/10/18 20:54
 */
@RestController
@RequestMapping("/order")
@Api(tags = "订单相关接口")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private AddressBookService addressBookService;


    @Autowired
    private OrderDetailService orderDetailService;

    @PostMapping("/submit")
    @ApiOperation("订单下单接口")
    @CacheEvict(value = "ordersCache", allEntries = true)
    public R<String> submit(@RequestBody Orders orders) {
        ordersService.submit(orders);
        return R.success("下单成功");
    }

    @GetMapping("/userPage")
    @ApiOperation("根据用户订单分页查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页记录数",required = true),
    })
    @Cacheable(value = "ordersCache",key = "#page+'_'+#pageSize+'userPage'")
    public R<Page> userPage(int page, int pageSize) {
        R<Page> pageR = ordersService.userPage(page, pageSize);
        return pageR;
    }

    @GetMapping("/page")
    @ApiOperation("订单分页查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页记录数",required = true),
            @ApiImplicitParam(name = "number",value = "套餐名称",required = false),
            @ApiImplicitParam(name = "beginTime",value = "开始时间",required = false),
            @ApiImplicitParam(name = "endTime",value = "结束时间",required = false)
    })
    @Cacheable(value = "ordersCache",key = "#page+'_'+#pageSize+'page'")
    public R<Page> page(int page,
                        int pageSize,
                        String number,
                        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime,
                        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        R<Page> pageR = ordersService.pageCat(page, pageSize, number, beginTime, endTime);
        return pageR;
    }

    @PutMapping
    @ApiOperation("订单修改状态接口")
    @CacheEvict(value = "ordersCache",allEntries = true)
    public R<String> updateStatus(@RequestBody Orders orders) {
        ordersService.updateWithStatus(orders);
        return R.success("已派送");
    }
}
